package handler

import (
	"encoding/json"
	"matrix/backend/db"
	"matrix/backend/util"
	"net/http"
	"strconv"
	"time"
)

// CcatalogHandler : 创建目录
func CcatalogHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	userName := r.Form.Get("userName")
	fileName := r.Form.Get("fileName")
	//  父路径
	path := r.Form.Get("path")
	var message util.FileMessage
	if userName != "" && path != "" && fileName != "" {
		thetime := time.Now().Format("2006-01-02 15:04:05")
		message.FileName = fileName
		message.FilePath = path
		message.Filetime = thetime
		message.Isdir = 1
		message.Category = 5
		message.FileID, message.Status = db.CreateCatalog(userName, fileName, thetime, path)
	} else {
		message.Status = 4
	}
	data, _ := json.Marshal(message)
	w.Header().Set("content-type", "text/json")
	w.Write(data)
}

// DeleteFileHandler : 删除文件
func DeleteFileHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	userName := r.Form.Get("userName")
	fileID := r.Form.Get("fileID")
	var message util.FileMessage
	if userName != "" && fileID != "0" {
		message.FileID, _ = strconv.Atoi(fileID)
		db.DeleteFile(&message, userName)
	}
	if message.Category == 5 {
		message.Isdir = 1
	}
	message.Filetime = time.Now().Format("2006-01-02 15:04:05")
	data, _ := json.Marshal(message)
	w.Header().Set("content-type", "text/json")
	w.Write(data)
}

// RenameFileHandler : 文件重命名
func RenameFileHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	var message util.FileMessage
	id := r.Form.Get("fileID")
	message.FileName = r.Form.Get("oldName")
	newName := r.Form.Get("newName")
	userName := r.Form.Get("userName")
	message.FilePath = r.Form.Get("path")
	category := r.Form.Get("category")
	if id != "0" && message.FileName != "" && userName != "" && newName != "" && category != "" && message.FilePath != "" {
		message.FileID, _ = strconv.Atoi(id)
		temp, _ := strconv.Atoi(category)
		message.Category = int8(temp)
		db.RenameFile(userName, &message, newName)
		data, _ := json.Marshal(message)
		w.Header().Set("content-type", "text/json")
		w.Write(data)
	}
}

// MoveFileHandler : 移动文件
func MoveFileHandler(w http.ResponseWriter, r *http.Request) {
	var message util.FileMessage
	r.ParseForm()
	id := r.Form.Get("fileID")
	message.FileName = r.Form.Get("fileName")
	newPath := r.Form.Get("newPath")
	userName := r.Form.Get("userName")
	message.FilePath = r.Form.Get("path")
	category := r.Form.Get("category")
	if id != "0" && message.FileName != "" && newPath != "" && userName != "" && category != "" && message.FilePath != "" {
		message.FileID, _ = strconv.Atoi(id)
		temp, _ := strconv.Atoi(category)
		message.Category = int8(temp)
		db.MoveFile(userName, &message, newPath)
		data, _ := json.Marshal(message)
		w.Header().Set("content-type", "text/json")
		w.Write(data)
	}
}

// FileListHandler : 文件列表
func FileListHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	userName := r.Form.Get("userName")
	parentPath := r.Form.Get("parentPath")
	var message []util.FileMessage
	if userName != "" && parentPath != "" {
		message = db.FileList(userName, parentPath)
	}
	data, _ := json.Marshal(message)
	w.Header().Set("content-type", "text/json")
	w.Write(data)
}
